﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Libs.Partner;

public partial class Pages_Partners_Company_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        Title = Title + " - Cập nhật cơ quan, đơn vị";
        if (IsPostBack) return;
        GetDrpFather();
        init();
    }

    private void GetDrpFather()
    {
        var _Company = new Company();
        drpFather.DataSource = _Company.GetList();
        drpFather.DataBind();
        drpFather.Items.Insert(0, new ListItem("N/A", "0"));
    }

    private void GetDrpOrder()
    {
        drpOrder.Items.Clear();
        drpOrder.Items.Insert(0, new ListItem("N/A (0)", "0"));
        var _Company = new Company();
        List<Company> list = _Company.GetList(Convert.ToInt32(drpFather.SelectedValue));
        for (int i = 0; i < list.Count; i++)
        {
            _Company = list[i];
            drpOrder.Items.Add(new ListItem(_Company.Alias + " (" + _Company.Order.ToString() + ")", _Company.Order.ToString()));
        }
    }

    private void init()
    {
        var _Company = new Company() { CompanyID = AppUtils.Request("id") };
        _Company = _Company.Get();
        if (_Company == null) Response.Redirect(Constant.ADMIN_PATH + Resources.Url.CompanyList);
        txtName.Text = _Company.Name;
        txtAlias.Text = _Company.Alias;
        drpFather.SelectedValue = _Company.FatherID.ToString();
        txtDescription.Text = _Company.Description;

        GetDrpOrder();
        drpOrder.SelectedValue = _Company.Order.ToString();
    }

    protected void btUpdate_Click(object sender, EventArgs e)
    {
        var _Company = new Company() { CompanyID = AppUtils.Request("id") };
        _Company = _Company.Get();
        _Company.Name = txtName.Text.Trim();
        _Company.Alias = txtAlias.Text.Trim();
        _Company.FatherID = Convert.ToInt32(drpFather.SelectedValue);
        if (Convert.ToInt32(drpOrder.SelectedValue) != _Company.Order) _Company.Order = Convert.ToInt32(drpOrder.SelectedValue) + 1;
        _Company.Description = txtDescription.Text.Trim();
        _Company.Update();

        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.CompanyList);
    }

    protected void btDelete_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.CompanyList);
    }

    protected void btCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.CompanyList);
    }

    protected void drpFather_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetDrpOrder();
    }
}
